package com.mediacloud.app.newsmodule.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.eventTrack.TXReportManager;
import com.mediacloud.app.assembly.util.Utility;
import com.mediacloud.app.assembly.views.CornerBlurView;
import com.mediacloud.app.model.component.ComponentItem;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.ArticleListData;
import com.mediacloud.app.model.news.CatalogItem;
import com.mediacloud.app.model.news.PayTipsUtilsKt;
import com.mediacloud.app.model.news.kt.TypeX;
import com.mediacloud.app.model.utils.DefaultBgUtil;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.activity.microlive.BaseRecyclerAdapter;
import com.mediacloud.app.newsmodule.adaptor.component.IExposure;
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils;
import com.tencent.mmkv.MMKV;

import java.util.List;

/**
 * Created by 陈凯 on 2017/9/21.
 */

public class RelatedVideoH extends BaseRelatedView implements IExposure {

    RecyclerView mRecyclerView;

    RecommendComponentAdapter mRelatedAdapter;
    private Context context;


    public RelatedVideoH(Context context) {
        super(context);
        this.context=context;
        initView();
    }

    public RelatedVideoH(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        initView();
    }

    public RelatedVideoH(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
        initView();
    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public RelatedVideoH(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.context=context;
    }

    @Override
    public void initView() {

        super.initView();

        mRecyclerView = mView.findViewById(R.id.hListView1);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mRecyclerView.getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRelatedAdapter = new RecommendComponentAdapter(mRecyclerView.getContext());
        mRecyclerView.setAdapter(mRelatedAdapter);

        mRelatedAdapter.setItemClickListener((index, isFromAdaptor, adapter) -> {
            ArticleItem data = listArticles.get(index);
            data.positionInList = index + 1;
           // TXReportManager.getInstance().app_jhapp_content_detail_rec_interact("点击查看详情",data);
                TXReportManager.getInstance().app_jhapp_explore_service_interact("点击查看", data,index);
            TXReportManager.getInstance().app_jhapp_content_detail_rec_interact("查看详情",data);
            NewsItemClickUtils.OpenItemNewsHandle(getContext(), data.getType(), data, new CatalogItem());
            if (context!=null&& context instanceof Activity){
                ((Activity)context).finish();
            }
        });
    }

    @Override
    int getViewLayOutId() {
        return R.layout.related_video;
    }

    @Override
    void onNetDataSuccess(ArticleListData data) {
        mRelatedAdapter.setData(data.articleList);

        mRelatedAdapter.notifyDataSetChanged();

    }

    @Override
    void onNetDataFailure(Object data) {

    }

    @Override
    public List<ArticleItem> getExposureArticles() {
        return TXReportManager.getExposureArticles(mRecyclerView,mRelatedAdapter.getData());
    }

    @Override
    public ComponentItem getExposureComponentItem() {
        return null;
    }

    private class RecommendComponentAdapter extends BaseRecyclerAdapter<ArticleItem> {
        private int singleWidth = 0;

        public RecommendComponentAdapter(Context context) {
            super(context);
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(getContext()).inflate(R.layout.related_colum_item_1003, parent, false);
            return new ViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
            if (singleWidth == 0) {
                singleWidth = (Utility.getScreenSize(mContext)[0]
                        - mContext.getResources().getDimensionPixelSize(R.dimen.dimen9) * 2
                ) / 2;
            }
            if (holder instanceof ViewHolder) {
                ViewHolder viewHolder = (ViewHolder) holder;

                viewHolder.itemView.setOnClickListener(v -> {
                    if (viewHolder.tx_title != null) viewHolder.tx_title.setTextColor(Color.parseColor("#666666"));
                    if (itemClickListener != null) {
                        itemClickListener.onItemClick(position, true, RecommendComponentAdapter.this);
                    }

                });
                viewHolder.onBindViewHolder(getData().get(position), singleWidth);
            }

        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        CornerBlurView bills;
        TextView tx_times;
        TextView tx_title;
        ImageView vrTag;
        ImageView startType;

        public ViewHolder(View itemView) {
            super(itemView);
        }

        public void onBindViewHolder(final ArticleItem data, final int singleWidth) {
            bills = itemView.findViewById(R.id.bills);
            tx_times = itemView.findViewById(R.id.tx_times);
            tx_title = itemView.findViewById(R.id.tx_title);
            startType = itemView.findViewById(R.id.start_type);
            vrTag = itemView.findViewById(R.id.vrTag);
            ViewGroup.LayoutParams lp = itemView.getLayoutParams();
            lp.width = singleWidth;
            itemView.setLayoutParams(lp);
            bills.load(data.getLogo(), true);
            if (data.getType() == TypeX.NEWS.AUDIO) {
                tx_times.setVisibility(View.VISIBLE);
                startType.setImageResource(R.drawable.icon_image_model_audio);
            } else if (data.getType() == TypeX.NEWS.VIDEO) {
                tx_times.setVisibility(View.VISIBLE);
                startType.setImageResource(R.drawable.icon_image_model_video);
            } else
                tx_times.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(data.getProp4())) {
                tx_times.setText(data.getProp4()+"");
            }else{
                tx_times.setText("00:00");
            }
            if (MMKV.defaultMMKV().getBoolean(data.getId() + "", false)) {
                tx_title.setTextColor(Color.parseColor("#666666"));
            } else {
                tx_title.setTextColor(Color.parseColor("#222222"));
            }
            PayTipsUtilsKt.payTips(tx_title, mContext.getResources().getText(R.string.pay_a_pei), data, mContext.getResources().getDimensionPixelSize(R.dimen.pay_label_size), mContext.getResources().getDimensionPixelSize(R.dimen.pay_label_bgradius));
            if (data.getType() == TypeX.NEWS.VR_VOD || data.getType() == TypeX.NEWS.VR_LIVE) {
                if (vrTag != null) {
                    vrTag.setVisibility(View.VISIBLE);
                    GradientDrawable gd = (GradientDrawable) vrTag.getBackground();
                    gd.setColor(DefaultBgUtil.getTintColor(getContext()));
                    vrTag.setBackground(gd);
                    if (data.getType() == TypeX.NEWS.VR_VOD){
                        vrTag.setImageResource(R.drawable.vr_tag_ico);
                    }else{
                        vrTag.setImageResource(R.drawable.vr_tag_live);
                    }
                }
            } else {
                if (vrTag != null) {
                    vrTag.setVisibility(View.GONE);
                }
            }

        }
    }
}